package com.wzqj.agent.web.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.wzqj.agent.common.ConfigHelper;
import com.wzqj.agent.common.SoaConfig;
import com.wzqj.agent.web.dao.DeviceDao;
import com.wzqj.agent.web.domain.Device;
import com.wzqj.agent.web.service.DeviceService;
import com.wzqj.wsp.response.ResponseValue;
import com.wzqj.wsp.response.ResultStatus;
import com.wzqj.wsp.soa.SOAUtil;
@Service("deviceService")
public class DeviceServiceImpl implements DeviceService {

	/**
     * 运营支撑平台服务
     */
    private static final String ACC_PROVIDER_APP_CODE = ConfigHelper.getProperty("ACC_PROVIDER_APP_CODE");
	/**
	 * 根据en查询设备
	 */

    private static final String ACC_DEVICE_EN = "devices/listByEns";
    private static final String ACC_MERCHANT_ENS = "merchant/getMerchantByEns";
    /**
	 * 绑定设备到店铺
	 */
	private static final String ACC_BIND_DEVICE_STORE = "merchant/bindDeviceToMerchant";
	
	@Autowired
	private DeviceDao deviceDao;
	/**
	 * 绑定单个设备en到店铺
	 */
	@Override
	public JSONObject bindDeviceToStore(String mcode, String en) {
		JSONObject soaParam = SoaConfig.getSoaParam();
		/********************判断en号查询店铺，判断该en号是否被绑定***************************/
		JSONObject enParams = new JSONObject();
		enParams.put("ens", en);
        ResponseValue enRes = SOAUtil.getResValFromSOA(ACC_PROVIDER_APP_CODE, ACC_MERCHANT_ENS,enParams,soaParam);
        if(enRes != null&&enRes.getStatus() == ResultStatus.SUCCESS_STATUS){
        	return null;
        }
        /******************************进行绑定操作**************************************/
        JSONObject params = new JSONObject();
        params.put("en", en);
        params.put("mcode", mcode);
        ResponseValue res = SOAUtil.getResValFromSOA(ACC_PROVIDER_APP_CODE, ACC_BIND_DEVICE_STORE, params,soaParam);
        //System.out.println("--bindDeviceToStore:"+res.toString());
        if(res != null && res.getStatus() == ResultStatus.SUCCESS_STATUS){
        	//存储店铺绑定设备关系
        	Device device = new Device();
        	device.setMcode(mcode);
        	device.setEn(en);
        	deviceDao.insert(device);
        	JSONObject data = new JSONObject();
        	data.put("mcode", res.getData());
        	//System.out.println("data:"+data);
            return data;
        }
		
		return null;
	}
	/**
	 * 批量绑定设备En（备用）
	@Override
	public JSONObject bindDevicesToStore(String mcode, String ens) {

		JSONObject data = new JSONObject();
		JSONObject soaParam = SoaConfig.getSoaParam();
        JSONObject params = new JSONObject();
        String[] enArr = ens.split(",");
        for (String en : enArr) {
        	params.put("en", en);
        	params.put("mcode", mcode);
        	ResponseValue res = SOAUtil.getResValFromSOA(ACC_PROVIDER_APP_CODE, ACC_BIND_DEVICE_STORE, params,soaParam);
        	//System.out.println("--bindDeviceToStore:"+res.toString());
        	if(res == null && res.getStatus() != ResultStatus.SUCCESS_STATUS){
        		return null;
        	}
        	Device device = new Device();
        	device.setMcode(mcode);
        	device.setEn(en);
        	deviceDao.insert(device);
        	data.put(en, res);
			
		}
        //data.put("mcode", res.getData());
        System.out.println("data:"+data);
        return data;
		
	}
	 */

}
